const { Serverlet } = require('./serverlet')
const url = require('url')
const querystring = require('querystring')

class func2 extends Serverlet {
  constructor (req, res) {
    super(req, res)
  }

  get (req, res) {
    let result = {
      code: 200,
      msg: '我是来自fun2的接口，恭喜你请求成功了',
      method: 'GET',
      detail: '来自func2'
    }
    res.setHeader('Access-Control-Expose-Headers', 'name')
    res.setHeader('name', 'cqg')
    res.setHeader('Content-Type', 'application/json;charset=utf8')
    res.end(JSON.stringify(result))
  }

  post (req, res) {
    var post = ''
    req.on('data', function (chunk) {
      post += chunk
    })
    req.on('end', function () {
      let obj = querystring.parse(post)
      let result = {
        code: 200,
        msg: '我是来自fun2的接口，恭喜你请求成功了,你的名字是' + obj.name + '我记住了',
        method: 'POST',
        detail: '来自func2'
      }
      res.setHeader('Content-Type', 'application/json;charset=utf8')
      res.end(JSON.stringify(result))
    })
  }
}

module.exports = {
  func2,
}
